﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace Photos
{
    public partial class EditPhotos : ModuleCompanyBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                int id = int.Parse(Request["id"]);

                var photo = (from p in DB.cp_Photos
                             where p.id == id
                             select p).FirstOrDefault();

                this.title.Text = photo.Title;
                this.descript.Text = photo.Description;
                this.img.ImageUrl = PathUrlImage + photo.Img;
                this.createdate.Text = photo.CreateDate.Value.ToString("dd/MM/yyyy");


            }
        }

        protected void btn_Click(object sender, EventArgs e)
        {
            int id = int.Parse(Request["id"]);

            var photo = (from p in DB.cp_Photos
                         where p.id == id
                         select p).FirstOrDefault();

            //kiem tra quyen hop le 
            string filename;
            if (file.HasFile)
            {
                filename = Guid.NewGuid() + file.FileName;
                file.SaveAs(PathPhysicalImage + filename);

                SaveThumb(filename, 90, 90);
            }
            else {

                filename = photo.Img;
            
            }

            photo.Img = filename;
            if (title.Text != null)
            {
                photo.Title = title.Text.Trim();
            }
            if (descript.Text != null)
            {
                photo.Description = descript.Text;
            }
            photo.CreateDate = DateTime.Now;

            DB.SubmitChanges();

            this.txtMs.Text = "đã cập nhật thành công";
        }
    }
}